package com.tsinghua.server.base.controller;
import lombok.extern.slf4j.Slf4j;
import org.python.util.PythonInterpreter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@RequestMapping(value = "exec")
@RestController
@Slf4j
public class PythonController {


    /**
     * 执行python脚本
     * @throws IOException
     * @throws InterruptedException
     */
    @GetMapping(value = "py")
    public void dealPy() throws IOException, InterruptedException {
        log.info("------");
//        Process proc = Runtime.getRuntime().exec("python /Users/eric/Desktop/test.py");
//        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
//        String line = null;
//        while ((line = in.readLine()) != null) {
//            System.out.println(line);
//        }
//        in.close();
//        proc.waitFor();

        PythonInterpreter interpreter = new PythonInterpreter();

        //执行Python脚本文件

        try {
            InputStream filepy = new FileInputStream("/Users/eric/Desktop/test.py");
            interpreter.execfile(filepy);
            filepy.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        interpreter.execfile("");
    }



}
